<template>
	<div>
		<Tabs>
			<TabPane label="优惠券发放设置">
				<Form ref="form" :model="formData" :rules="rules" label-position="top">
					<FormDescItem label="限制获取数量" prop="amount_limit" desc="设置每个用户最多能够获得多少张优惠券，必须为正整数，且数字大于0">
						<Input v-model="formData.amount_limit" number :disabled="formData.amount_limit_disabled">
							<template slot="append">
								<Checkbox label="不设限制" v-model="formData.amount_limit_disabled">不设限制</Checkbox>
							</template>
						</Input>
					</FormDescItem>
					<FormDescItem label="用户获取限制" desc="设置可获取优惠券的用户，可指定会员等级和会员组">
						<RadioGroup v-model="formData.discount_type">
			                <Radio :label="1">不限制</Radio>
			                <Radio :label="2">指定等级</Radio>
			                <Radio :label="3">指定会员组</Radio>
			            </RadioGroup>
					</FormDescItem>
					<FormItem>
						<Button type="primary" @click="handleSubmit" :loading="commiting">保存设置并启用</Button>
						<Button @click="handleBack" style="margin-left: 10px;">返回列表</Button>
					</FormItem>
				</Form>
			</TabPane>
		</Tabs>
	</div>
</template>

<script>
import Util from '@/libs/util.js';
import FormDescItem from '@/components/form/FormDescItem.vue';
export default {
	name: 'couponEdit',
	components: {
		FormDescItem
	},
	data() {
		return {
			commiting: false,
			showGoodsModal: false,
			ids: [],
			brandLists: [],
			targetBrands: [],
			categoryLists: [],
			selectedGoods: [],
			formData: {
				id: null,
				type: 1,
				name: '',
				content: '',
				status: 1,
				total: 1,
				discount_type: 1,
				discount_money: 10,		//
				discount_scale: 90,		// type为1时为优惠金额，为3时为折扣
				discount_min: 1,
				discount_max: 10,
				use_limit: 1,			// 使用限制
				indate_type: 2,			// 有效期类型 1 固定日期 2 领取次日N天
				indate_rangetime: [],	// indate[type]为1时，固定日期，需要起始日期
				indate_date: 1,		// indate[type]为2时，领取次日N天
				amount_limit: 1,		// 订单使用门槛金额，0为立减
				amount_limit_disabled: false,
				is_advance: 0,			// 是否提前领取
			},
			rules: {
				name: [
					{required: true, message: '请输入优惠券名称', trigger: 'blur'}
				],
				amount_limit: [
					{type: 'number', message: '请输入数字类型', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		
	},
	methods: {
		// 返回上一级
		handleBack() {
			this.$router.back();
		},
		// 提交
		handleSubmit() {
			this.$refs.form.validate((valid) => {
                if (valid) {
                	let form = this.formData;
                	let topCate = this.categoryLists[0];
                	
                	if(this.commiting) return false;
                	
                	// 验证是否选择日期
                	if(form.indate_type == 1 && form.indate_rangetime.length == 0) {
                		this.$Message.error('请选择优惠券有效期');
						return false;
                	}
                	
                	// 校验品牌、品类、单品
                	if(form.type == 2 && this.targetBrands.length == 0) {
            			this.$Message.error('请选择参与品牌');
						return false;
            		}else if(form.type == 3) {
            			if(!topCate.checked && !topCate.indeterminate) {
            				this.$Message.error('请选择参与品类');
							return false;
            			}
            		}else if(form.type == 4) {
            			if(this.selectedGoods.length == 0) {
                			this.$Message.error('请至少选择一个参与的单品');
							return false;
                		}
            		}
            		
                    let params = {
                    	name: form.name,
                    	type: form.type,
                    	status: form.status,
                    	total: form.total,
                    	content: form.content,
                    	discount: {
                    		type: form.discount_type
                    	},
                    	use_limit: form.use_limit,
                    	indate: {
                    		type: form.indate_type
                    	},
                    	amount_limit: (form.amount_limit_disabled ? 0 : form.amount_limit)
                    }
                    
                    // 优惠券类型
                    if(form.discount_type == 1) {
                    	params.discount.scale = form.discount_money;
                    	params.discount_scale = form.discount_money;
                    }else if(form.discount_type == 2) {
                    	params.discount.min = form.discount_min;
                    	params.discount.max = form.discount_max;
                    }else{
                    	params.discount.scale = form.discount_scale;
                    	params.discount_scale = form.discount_scale;
                    }
                    
                    // 有效期类型
                    if(form.indate_type == 1) {
                    	params.indate.date = '';
                    	params.indate.start_time = form.indate_rangetime[0];
                    	params.indate.end_time = form.indate_rangetime[1];
                    	params.is_advance = form.is_advance;
                    }else{
                    	params.indate.date = form.indate_date;
                    	params.indate.start_time = '';
                    	params.indate.end_time = '';
                    	params.is_advance = 0;
                    }
                    
                    // 
                    if(form.type != 1) {
                    	params.rule = {};
                    	let ids = this.selectedGoods.map(item => {
                    		return item.sku_id;
                    	});
                    	if(form.type == 2) {
	                    	params.rule.brand_ids = this.targetBrands;
	                    	params.rule.except_ids = ids;
	                    }else if(form.type == 3) {
	                    	let cates = topCate.children;
	                    	let checkedCates = this.handleGetCheckedCategory(cates);
	                    	params.rule.cat_ids = this.handleGetCategoryIds(checkedCates);
	                    	params.rule.except_ids = ids;
	                    }else if(form.type == 4) {
	                    	params.rule.sku_ids = ids;
	                    }
                    }
                    
                    this.commiting = true;
                    Util.ajax.post('/v1/coupon.coupon/create', params).then((res) => {
                    	this.commiting = false;
                    	this.$Message.success({
                    		content: '添加成功',
                    		onClose: () => {
                    			this.handleBack();
                    		}
                    	})
                    }).catch(() => {
                    	this.commiting = false;
                    })
                }
            })
		}
	}
}
</script>